<?php
/**
 * 
 * @todo description of this class
 * 
 * @author Alton Crossley <crossleyframework@nogahidebootstrap.com>
 * @package Crossley Framework
 *  
 * @copyright Copyright (c) 2003-2009, Nogaide BootStrap INC. All rights reserved.
 * @license BSD http://opensource.org/licenses/bsd-license.php
 * @version $Id:$
 * 
 */
class X_Filter_Alnum implements X_Filter_Interface
{
    /**
     * if white space is allowed
     *
     * @var boolean
     */
    public $bAllowWhiteSpace;
    
    /**
     * permited characters
     *
     * @var unknown_type
     */
    public $aPermitted;
    
    /**
     * Sets default option values for this instance
     *
     * @param  boolean $allowWhiteSpace
     * @return void
     */
    public function __construct($bAllowWhiteSpace = false, Array $aPermitted = array())
    {
        $this->bAllowWhiteSpace = (boolean) $bAllowWhiteSpace;
        $this->aPermitted = $aPermitted;
    }
    
    /**
     * Defined by Zend_Filter_Interface
     *
     * Returns the string $value, removing all but alphabetic and digit characters
     *
     * @param  string $value
     * @return string
     */
    public function filter($sValue)
    {
        $sWhiteSpace = ($this->bAllowWhiteSpace) ? '\s' : '';
        
        $sExtraChars = (empty($this->aPermitted)) ? '' : trim(implode('', $this->aPermitted));
        
        $sPattern = '/[^a-zA-Z0-9' . $sWhiteSpace . $sExtraChars . ']/u';
        
        return preg_replace($sPattern, '', (string) $sValue);
    }
    
    static function go($xValue, $bAllowWhiteSpace = false, Array $aPermitted = array())
    {
        $sSelf = __CLASS__;
        $oFileter = new $sSelf($bAllowWhiteSpace, $aPermitted);
        
        if (is_array($xValue))
        {
            $aFiltered = array();
            foreach ($xValue as $sKey => $xValue)
            {
                $aFiltered[$sKey] = $oFileter->filter((string)$xValue);
            }
            return $aFiltered;
        }
        else
        {
            return $oFileter->filter((string)$xValue);
        }
        
    }
}
